import { fetchData } from "../utils/fetchCollections/fetch";
import { fetchDataBeforeLogin } from "../utils/fetchCollections/login";

export interface UserInfo {
  phone: string;
  areaCode: string;
  token?: string;
  nickname?: string;
  id?: string;
  money?: number;
}

export interface LoginResult {
  token: string;
  phone: string;
}

export class UserService {
  // 发送验证码
  static async sendVerificationCode(phone: string) {
    try {
      // TODO: 实现实际的验证码发送逻辑
      await fetchDataBeforeLogin(
        "https://pocketapi.48.cn/user/api/v1/sms/send2",
        {
          mobile: phone,
          area: "86",
        }
      );
    } catch (error) {
      console.error("Failed to send verification code:", error);
      throw new Error("发送验证码失败");
    }
  }

  // 使用验证码登录
  static async loginWithCode(phone: string, code: string) {
    try {
      const loginResponse = await fetchDataBeforeLogin(
        "https://pocketapi.48.cn/user/api/v1/login/app/mobile/code",
        {
          mobile: phone,
          code: code,
        }
      );
      return loginResponse?.content;
    } catch (error) {
      console.error("Login failed:", error);
      throw error;
    }
  }

  static async loginWithToken(token: string) {
    try {
      const res = await fetchData("https://pocketapi.48.cn/user/api/v1/user/info/reload", {"from":"appstart"}, token)
      if (!res?.success) {
        return null
      }
      return {
        ...res?.data?.content,
        ...res?.content
      }
    } catch(error) {
      console.error("Login failed:", error);
      throw error;
    }
  }
  // 登出
  static async logout() {
    // TODO: Implement logout logic
  }

  // 检查登录状态
  static async checkLoginStatus() {
    // TODO: Implement checkLoginStatus logic
    return false;
  }
}
